"use strict";

const { LoaderOptionsPlugin, LoaderTargetPlugin } = require("@rspack/core");
const { RuntimePlugin } = require("@rspack/core");

/**
 * @import { Compiler, WebpackPluginInstance, RspackPluginInstance } from "@rspack/core";
 */
/**
 * @implements {WebpackPluginInstance}
 */
class DevOnlyPlugin {
  /**
   * @readonly
   */
  static PLUGIN_NAME = "DEV_ONLY";

  /**
   * @param {Compiler} compiler
   * @returns {void}
   */
  apply(compiler) {
    compiler.hooks.compilation.tap(DevOnlyPlugin.PLUGIN_NAME, (compilation) => {
      compilation.hooks.buildModule.tap(DevOnlyPlugin.PLUGIN_NAME, (m) => {
        const resource = m.nameForCondition();
        if (resource && !/node_modules/.test(resource)) {
          // 这里表示当前模块是 JavaScript 文件
          // console.log("context: ", m.context);
          // console.log("identifier: ", m.identifier());
          // console.log("resource: ", m?.resource);
          !resource.startsWith("data:") &&
            console.log("DEV_ONLY::resource", resource);
        }
      });
      // compiler.hooks.make.tap(PLUGIN_NAME, (compilation) => {
      //   compilation.getLogger("loader").info("DEV_ONLY: make");
      // })
    });
    // compiler.hooks.run.tap(DevOnlyPlugin.PLUGIN_NAME, (compiler) => {
    //   compiler
    //     .getInfrastructureLogger(DevOnlyPlugin.PLUGIN_NAME)
    //     .info("run");
    //   // compiler.cache
    // });
    // compiler.hooks.emit.tap(DevOnlyPlugin.PLUGIN_NAME, (compilation) => {
    //   (compilation.getLogger
    //     ? compilation.getLogger(DevOnlyPlugin.PLUGIN_NAME)
    //     : console
    //   ).info("emit");
    // });
    // const rules = compiler.options.module?.rules;
    // console.log("rules", rules);
  }
}

module.exports = { DevOnlyPlugin };
